#include<iostream>
#include<boost/asio.hpp>


using namespace boost;
//服务端
/*
1、创建socket
2、绑定自身ip和端口
3、开始监听

4、收到连接请求：
4.1创建新的socket
4.2 read() and write()
4.3 read()到close()信号 断开连接
*/

//服务器创建终端节点
int server_end_point()
{
    unsigned short port = 3333;
    boost::asio::ip::address ip = boost::asio::ip::address_v4::any();

    boost::asio::ip::tcp::endpoint ep(ip,port);

    return 0;
}

//客户端创建终端节点
int client_end_point()
{
    unsigned short port = 3333;
    std::string origin_ip = "127.0.0.1";

    boost::system::error_code er;

    boost::asio::ip::address ip = boost::asio::ip::address::from_string(origin_ip,er);

    if (er.value() != 0)
    {
         std::cout
            << "Failed to parse the IP address. Error code = "
            << er.value() << ". Message: " << er.message();
        return er.value();
    }

    boost::asio::ip::tcp::endpoint ep(ip,port);

    return 0;
}


//创建socket
/*
1、创建上下文iocontext，
2、选择协议，
3、生成socket，
4、打开socket。
*/

int create_tcp_socket()
{
    boost::asio::io_context  ios;
    // Step 2. Creating an object of 'tcp' class representing
    // a TCP protocol with IPv4 as underlying protocol.
    boost::asio::ip::tcp protocol = boost::asio::ip::tcp::v4();
    // Step 3. Instantiating an active TCP socket object.
    boost::asio::ip::tcp::socket sock(ios);
    // Used to store information about error that happens
    // while opening the socket.
    boost::system::error_code ec;
    // Step 4. Opening the socket.
    sock.open(protocol, ec);
    if (ec.value() != 0) {
        // Failed to open the socket.
        std::cout
            << "Failed to open the socket! Error code = "
            << ec.value() << ". Message: " << ec.message();
        return ec.value();
    }
    return 0;
}


//服务器创建接收新连接的socket
int  create_acceptor_socket() {
    // Step 1. An instance of 'io_service' class is required by
        // socket constructor. 
    asio::io_context ios;
    // Step 2. Creating an object of 'tcp' class representing
            // a TCP protocol with IPv6 as underlying protocol.
    asio::ip::tcp protocol = asio::ip::tcp::v6();
    // Step 3. Instantiating an acceptor socket object.
    asio::ip::tcp::acceptor acceptor(ios);
    // Used to store information about error that happens
    // while opening the acceptor socket.
    boost::system::error_code ec;
    // Step 4. Opening the acceptor socket.
    acceptor.open(protocol, ec);
    if (ec.value() != 0) {
        // Failed to open the socket.
        std::cout
            << "Failed to open the acceptor socket!"
            << "Error code = "
            << ec.value() << ". Message: " << ec.message();
        return ec.value();
    }
    return 0;
}

//对于acceptor类型的socket，服务器要将其绑定到指定的端点,所有连接这个端点的连接都可以被接收到。
int  bind_acceptor_socket() {
    // Step 1. Here we assume that the server application has
        // already obtained the protocol port number.
    unsigned short port_num = 3333;
    // Step 2. Creating an endpoint.
    asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),
        port_num);
    // Used by 'acceptor' class constructor.
    asio::io_context  ios;
    // Step 3. Creating and opening an acceptor socket.
    asio::ip::tcp::acceptor acceptor(ios, ep.protocol());
    boost::system::error_code ec;
    // Step 4. Binding the acceptor socket.
    acceptor.bind(ep, ec);
    // Handling errors if any.
    if (ec.value() != 0) {
        // Failed to bind the acceptor socket. Breaking
        // execution.
        std::cout << "Failed to bind the acceptor socket."
            << "Error code = " << ec.value() << ". Message: "
            << ec.message();
        return ec.value();
    }
    return 0;
}


//作为客户端可以连接服务器指定的端点进行连接
int connect_to_end() {
    // Step 1. Assume that the client application has already
            // obtained the IP address and protocol port number of the
            // target server.
    std::string raw_ip_address = "127.0.0.1";
    unsigned short port_num = 3333;
    try {
        // Step 2. Creating an endpoint designating 
        // a target server application.
        asio::ip::tcp::endpoint
            ep(asio::ip::address::from_string(raw_ip_address),
                port_num);
        asio::io_context ios;
        // Step 3. Creating and opening a socket.
        asio::ip::tcp::socket sock(ios, ep.protocol());
        // Step 4. Connecting a socket.
        sock.connect(ep);
        // At this point socket 'sock' is connected to 
        // the server application and can be used
        // to send data to or receive data from it.
    }
    // Overloads of asio::ip::address::from_string() and 
    // asio::ip::tcp::socket::connect() used here throw
    // exceptions in case of error condition.
    catch (system::system_error& e) {
        std::cout << "Error occured! Error code = " << e.code()
            << ". Message: " << e.what();
        return e.code().value();
    }
}

//当有客户端连接时，服务器需要接收连接

int accept_new_connection(){
    // The size of the queue containing the pending connection
            // requests.
    const int BACKLOG_SIZE = 30;
    // Step 1. Here we assume that the server application has
    // already obtained the protocol port number.
    unsigned short port_num = 3333;
    // Step 2. Creating a server endpoint.
    asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(),
        port_num);
    asio::io_context  ios;
    try {
        // Step 3. Instantiating and opening an acceptor socket.
        asio::ip::tcp::acceptor acceptor(ios, ep.protocol());
        // Step 4. Binding the acceptor socket to the 
        // server endpint.
        acceptor.bind(ep);
        // Step 5. Starting to listen for incoming connection
        // requests.
        acceptor.listen(BACKLOG_SIZE);
        // Step 6. Creating an active socket.
        asio::ip::tcp::socket sock(ios);
        // Step 7. Processing the next connection request and 
        // connecting the active socket to the client.
        acceptor.accept(sock);
        // At this point 'sock' socket is connected to 
        //the client application and can be used to send data to
        // or receive data from it.
    }
    catch (system::system_error& e) {
        std::cout << "Error occured! Error code = " << e.code()
            << ". Message: " << e.what();
        return e.code().value();
    }
}


int main(int argc, char const *argv[])
{
    
    return 0;
}
